package com.github.prontera.service;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.domain.ClientInvitation;
import com.github.prontera.domain.ClientTask;
import com.github.prontera.domain.ClientTaskCount;
import com.github.prontera.domain.ClientVideo;
import com.github.prontera.domain.SystemAccount;
import com.github.prontera.domain.Task;
import com.github.prontera.domain.enums.TaskTypeEnum;
import com.github.prontera.persistence.ClientInvitationMapper;
import com.github.prontera.persistence.ClientTaskCountMapper;
import com.github.prontera.persistence.ClientTaskMapper;
import com.github.prontera.persistence.ClientVideoMapper;
import com.github.prontera.persistence.CrudMapper;
import com.github.prontera.persistence.TaskMapper;
import com.github.prontera.util.OrderNoGenerator;
import com.github.prontera.util.Page;

/**
 * 任务
 */
@Service
public class TaskService extends CrudServiceImpl<Task>{
    
	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	private ClientTaskMapper clientTaskMapper;
	@Autowired
	private ClientTaskCountMapper clientTaskCountMapper;
	@Autowired
	private SystemAccountService systemAccountService;
	
  	@Autowired
    public TaskService(CrudMapper<Task> mapper) {
        super(mapper);
    }
	
  	public Integer schdulerUpdateTaskStart(){
  		return taskMapper.schdulerUpdateTaskStart();
  	}
  	
  	@Autowired
  	private ClientVideoMapper clientVideoMapper;
  	@Autowired
  	private ClientInvitationMapper clientInvitationMapper;
  	
  	/**
  	 * 这个定时任务是每天凌晨00:01开始跑
  	 */
  	public void schdulerUpdateEnd(){
  		List<Task> tasks= taskMapper.findTaskEnd();
  		List<Long> ids=new ArrayList<Long>(10);
  		for (Task task : tasks) {
			Integer taskComplateStep=0;
			String[] claimTypes= task.getClaimTypes().split("[,]");
			String[] claimValues= task.getClaimValues().split("[,]");
			ClientTaskCount clientTaskCountObj=task.getClientTaskCount();
			ClientTask clientTaskObj=task.getClientTask();
			
			for (int l = 0; l < claimValues.length; l++) {
				String claimType=claimTypes[l];
				String claimValue=claimValues[l];
				
			    //更新已完成订单
	  			if(Integer.parseInt(claimType)==TaskTypeEnum.ONE.getCode() && (clientTaskCountObj.getDaySigned().intValue()>=Integer.parseInt(claimValue))){
	  				taskComplateStep=taskComplateStep+1;
	  				break;
//  			TWO(2, "每日最少查看X个达人视频"),//定时任务
//  			THREE(3, "在任务周期内，至少邀请X名好友"),//定时任务
//  			FOUR(4, "在任务周期内，至少邀请X名消费用户"),//定时任务
//  			FIVE(5, "在任务周期内，至少完成X笔消费返利订单"),//定时任务
//  			SIX(6, "在任务周期内，至少邀请X名技能达人");//定时任务
	  			}else if(Integer.parseInt(claimType)==TaskTypeEnum.TWO.getCode()){
	  				ClientVideo clientVideo=new ClientVideo();
	  				clientVideo.setStartTime_(clientTaskObj.getCreateTime());
	  				clientVideo.setEndTime_(clientTaskObj.getEndTime());
	  				clientVideo.setClientId(clientTaskObj.getClientId());
	  				clientVideo.setCount(Integer.parseInt(claimValue));
	  				long count=clientVideoMapper.countBetweenDayLookVideo(clientVideo);
	  				if(count==new Long(task.getMaxDay()).intValue()){
	  					taskComplateStep=taskComplateStep+1;
	  					clientTaskCountObj.setVideoCount(task.getMaxDay().intValue());
	  				}
	  				break;
	  			//THREE(3, "在任务周期内，至少邀请X名好友"),//定时任务
	  			}else if(Integer.parseInt(claimType)==TaskTypeEnum.THREE.getCode() ){ //TODO  还需要更新时间区间内已使用数据
	  				ClientInvitation clientInvitation=new ClientInvitation();
	  				clientInvitation.setStartTime_(clientTaskObj.getCreateTime());
	  				clientInvitation.setEndTime_(clientTaskObj.getEndTime());
	  				clientInvitation.setClientId(clientTaskObj.getClientId());
	  				long count=clientInvitationMapper.countBetweenDay(clientInvitation);
	  				if(count==Long.parseLong(claimValue)){
	  					taskComplateStep=taskComplateStep+1;
	  					clientTaskCountObj.setInvitationCount(task.getMaxDay().intValue());
	  				}
	  				break;
	  			//FOUR(4, "在任务周期内，至少邀请X名消费用户"),//定时任务
				}else if(Integer.parseInt(claimType)==TaskTypeEnum.FOUR.getCode() ){ //TODO  还需要更新时间区间内已使用数据
//					select * from u_client_invitation uci
//					left join o_talent_order oto on oto.client_id=uci.passive_client_id
//					where uci.client_id=1 and oto.accept_status=2 and a oto.update_time> '2018-02-09 10:04:32' and oto.update_time < '2018-03-06 11:36:10' 
//					GROUP BY uci.passive_client_id
//					limit 0,1
					taskComplateStep=taskComplateStep+1;
	  				break;
	  			//FIVE(5, "在任务周期内，至少完成X笔消费返利订单"),//定时任务
				}else if(Integer.parseInt(claimType)==TaskTypeEnum.FIVE.getCode() ){ //TODO  还需要更新时间区间内已使用数据
	  				taskComplateStep=taskComplateStep+1;
//	  				select * from  o_talent_order oto 
//	  				where oto.client_id=1 and oto.status=3 and a oto.update_time> '2018-02-09 10:04:32' and oto.update_time < '2018-03-06 11:36:10' 
//	  				limit 0,1
	  				break;
	  			//SIX(6, "在任务周期内，至少邀请X名技能达人");//定时任务
				}else if(Integer.parseInt(claimType)==TaskTypeEnum.SIX.getCode()){  //TODO  还需要更新时间区间内已使用数据
	  				taskComplateStep=taskComplateStep+1;
//	  				select * from u_client_invitation uci
//	  				left join u_client uc on uc.id=uci.passive_client_id
//	  					where uci.client_id=1 and oto.accept_status=2 and a oto.update_time> '2018-02-09 10:04:32' and oto.update_time < '2018-03-06 11:36:10' 
//	  						limit 0,1
	  				break;
				}
//	  			if(taskComplateStep.intValue()==claimValues.length){//表示任务完成
//	  				ClientTask clientTask=new ClientTask();
//	  				clientTask.setClientId(clientId);
//	  				clientTask.setTaskId(task.getId());
//	  				clientTask.setStatus(2);
//	  				if(clientTaskMapper.update(clientTask)<=0){
//	  					Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "更新客户任务状态失败!");
//	  				}
//	  			}
			}
		}
//  		if(ids.size()>0){
//  			taskMapper.schdulerUpdateClientTaskEnd(ids);
//  			taskMapper.schdulerUpdateTaskEnd(ids);
//  		}
  	}
  	
  	/**
  	 * 获取我的任务
  	*/
  	public List<Task> findClientTask(Task task){
  		return taskMapper.findClientTask(task);
  	}
  	
  	public Task getClientTask(Task task) {
		return taskMapper.getClientTask(task);
	}
  	
	public Task getClientOneAccordTask(Long clientId, TaskTypeEnum taskTypeEnum) {
		Task findTask=new Task();
		findTask.setClientId(clientId);
		findTask.setClaimType(taskTypeEnum);
		findTask.setPageIndex(1);
		findTask.setPageSize(1);
		List<Task> tasks= taskMapper.getClientAccordTask(findTask);
		if(tasks!=null&&tasks.size()>0){
			 return tasks.get(0);
		}
		return null;
	}
  	
  	public List<Task> getClientAccordTask(Long clientId,TaskTypeEnum taskTypeEnum) {
  		Task findTask=new Task();
		findTask.setClientId(clientId);
		findTask.setClaimType(taskTypeEnum);
  		return taskMapper.getClientAccordTask(findTask);
  	}
  	
	/**
	 *  typeId : 
	 *	1）	连续30天，每日签到
	 *	2）	连续30天，每日签到并查看一个达人视频
	 *	3）	在任务周期内，至少邀请10名好友   （是要隶属于邀请者即可，不管活粉死粉）
	 *	4）	在任务周期内，至少邀请30名好友
	 *	5）	在任务周期内，至少邀请5名消费用户   线下预约达人订单成功才算一个消费）
	 *	6）	在任务周期内，至少完成5笔消费返利订单    本人至少完成5笔达人技能线下预约
	 *	7）	在任务周期内，至少邀请5名技能达人（注册用户先技能激活，再发布技能，才算成功）  
  	 * 分配结果
  	*/
  	public boolean pushTaskResult(TaskTypeEnum taskTypeEnum,Long clientId,Boolean hasReset){
  		Task findTask=new Task();
  		findTask.setClientId(clientId);
  		findTask.setClaimTypes(taskTypeEnum.getCode()+"");
  		List<Task> tasks=taskMapper.findClientTask(findTask);
  		if(tasks!=null && tasks.size()!=0){
  			for (Task task : tasks) {
  				Integer taskComplateStep=0;
  				String[] claimTypes= task.getClaimTypes().split("[,]");
  				String[] claimValues= task.getClaimValues().split("[,]");
  	  			ClientTaskCount clientTaskCountObj=task.getClientTaskCount();
  	  			
  	  			ClientTaskCount clientTaskCount=new ClientTaskCount();
  	  			clientTaskCount.setClientId(clientId);
  	  			clientTaskCount.setTaskId(task.getId());
  	  			
  	  			if(taskTypeEnum==TaskTypeEnum.ONE && clientTaskCountObj.getDaySigned().intValue()<Integer.parseInt(claimValues[0])){
  	  				clientTaskCount.setDaySigned(1);
  	  				if(hasReset){
  	  					clientTaskCountMapper.updateCount2(clientTaskCount);
  	  				}else{
	  	  				clientTaskCountMapper.updateCount(clientTaskCount);
  	  				}
  	  				break;
  	  			}else if(taskTypeEnum==TaskTypeEnum.TWO && clientTaskCountObj.getVideoCount().intValue()<Integer.parseInt(claimValues[1])){
  	  				clientTaskCount.setVideoCount(1);
  	  				if(hasReset){
	  					clientTaskCountMapper.updateCount2(clientTaskCount);
	  				}else{
	  					clientTaskCountMapper.updateCount(clientTaskCount);
	  				}
  	  				break;
  	  			}else if(taskTypeEnum==TaskTypeEnum.THREE  && clientTaskCountObj.getInvitationCount().intValue()<Integer.parseInt(claimValues[2])){
  	  				clientTaskCount.setInvitationCount(1);
  	  				if(hasReset){
	  					clientTaskCountMapper.updateCount2(clientTaskCount);
	  				}else{
	  					clientTaskCountMapper.updateCount(clientTaskCount);
	  				}	
  	  				break;
  				}else if(taskTypeEnum==TaskTypeEnum.FOUR  && clientTaskCountObj.getConsumerCount().intValue()<Integer.parseInt(claimValues[3])){
  					clientTaskCount.setConsumerCount(1);
  	  				if(hasReset){
	  					clientTaskCountMapper.updateCount2(clientTaskCount);
	  				}else{
	  					clientTaskCountMapper.updateCount(clientTaskCount);
	  				}
  	  				break;
  				}else if(taskTypeEnum==TaskTypeEnum.FIVE && clientTaskCountObj.getReturnConsumerCount().intValue()<Integer.parseInt(claimValues[4])){
  					clientTaskCount.setReturnConsumerCount(1);
  	  				if(hasReset){
	  					clientTaskCountMapper.updateCount2(clientTaskCount);
	  				}else{
	  					clientTaskCountMapper.updateCount(clientTaskCount);
	  				}
  	  				clientTaskCountMapper.updateCount(clientTaskCount);
  	  				break;
  				}else if(taskTypeEnum==TaskTypeEnum.SIX && clientTaskCountObj.getTalentCount().intValue()<Integer.parseInt(claimValues[5])){ 
  					clientTaskCount.setTalentCount(1);
  	  				if(hasReset){
	  					clientTaskCountMapper.updateCount2(clientTaskCount);
	  				}else{
	  					clientTaskCountMapper.updateCount(clientTaskCount);
	  				}
  	  				clientTaskCountMapper.updateCount(clientTaskCount);
  	  				break;
  				}
  	  			
  	  			for (int l = 0; l < claimValues.length; l++) {
  	  				String claimType=claimTypes[l];
  	  				String claimValue=claimValues[l];
  	  				
  	  			//更新已完成订单
  	  	  			if(Integer.parseInt(claimType)==TaskTypeEnum.ONE.getCode() && (clientTaskCountObj.getDaySigned().intValue()>=Integer.parseInt(claimValue)
  	  	  						|| (taskTypeEnum.getCode()==TaskTypeEnum.ONE.getCode() && clientTaskCountObj.getDaySigned().intValue()==(Integer.parseInt(claimValue)-1)))){
  		  				taskComplateStep=taskComplateStep+1;
  		  				break;
  		  			}else if(Integer.parseInt(claimType)==TaskTypeEnum.TWO.getCode()&&  (clientTaskCountObj.getVideoCount().intValue()>=Integer.parseInt(claimValue)
  		  					||(taskTypeEnum.getCode()==TaskTypeEnum.TWO.getCode() && clientTaskCountObj.getVideoCount().intValue()==(Integer.parseInt(claimValue)-1)))){
  		  				taskComplateStep=taskComplateStep+1;
  		  				break;
  		  			}else if(Integer.parseInt(claimType)==TaskTypeEnum.THREE.getCode() &&  (clientTaskCountObj.getInvitationCount().intValue()>=Integer.parseInt(claimValue)
  		  					||(taskTypeEnum.getCode()==TaskTypeEnum.THREE.getCode() &&clientTaskCountObj.getInvitationCount().intValue()==(Integer.parseInt(claimValue)-1)))){
  		  				taskComplateStep=taskComplateStep+1;
  		  				break;
  					}else if(Integer.parseInt(claimType)==TaskTypeEnum.FOUR.getCode() &&  (clientTaskCountObj.getConsumerCount().intValue()>=Integer.parseInt(claimValue)
  							||(taskTypeEnum.getCode()==TaskTypeEnum.FOUR.getCode() &&clientTaskCountObj.getConsumerCount().intValue()==(Integer.parseInt(claimValue)-1)))){
  						taskComplateStep=taskComplateStep+1;
  		  				break;
  					}else if(Integer.parseInt(claimType)==TaskTypeEnum.FIVE.getCode() &&  (clientTaskCountObj.getReturnConsumerCount().intValue()>=Integer.parseInt(claimValue)
  							||(taskTypeEnum.getCode()==TaskTypeEnum.FIVE.getCode() &&clientTaskCountObj.getReturnConsumerCount().intValue()==(Integer.parseInt(claimValue)-1)))){
  		  				taskComplateStep=taskComplateStep+1;
  		  				break;
  					}else if(Integer.parseInt(claimType)==TaskTypeEnum.SIX.getCode() &&  (clientTaskCountObj.getTalentCount().intValue()>=Integer.parseInt(claimValue)
  							|| (taskTypeEnum.getCode()==TaskTypeEnum.SIX.getCode() &&clientTaskCountObj.getTalentCount().intValue()==(Integer.parseInt(claimValue)-1)))){ 
  		  				taskComplateStep=taskComplateStep+1;
  		  				break;
  					}
  	  	  			if(taskComplateStep.intValue()==claimValues.length){//表示任务完成
  	  	  				ClientTask clientTask=new ClientTask();
  	  	  				clientTask.setClientId(clientId);
  	  	  				clientTask.setTaskId(task.getId());
  	  	  				clientTask.setStatus(2);
  	  	  				if(clientTaskMapper.update(clientTask)<=0){
  	  	  					Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "更新客户任务状态失败!");
  	  	  				}
  	  	  			}
				}
  			}
  		}
		return true;
  	}
  	
  	
  	/**
  	 * 领取任务
  	*/
  	public boolean insert(ClientTask clientTask) {
  		if(clientTaskMapper.insert(clientTask)<=0){
  			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "插入数据错误");
  		}
  		ClientTaskCount clientTaskCount=new ClientTaskCount();
  		clientTaskCount.setTaskId(clientTask.getTaskId());
  		clientTaskCount.setClientId(clientTask.getClientId());
  		if(clientTaskCountMapper.insert(clientTaskCount)<=0){
  			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "插入数据错误");
  		}
		return true;
	}
 
  	@Transactional(propagation = Propagation.REQUIRED)
	public boolean buyTask(Task task, Long clientId,Integer number) {
		Task updateTask=new Task();
		updateTask.setId(task.getId());
		updateTask.setSignedNumber(1);
		updateTask.setActualNumber(number);
		if(taskMapper.updateCount(updateTask)>0) {//更新已购买量成功
			for (int i = 0; i <number; i++) {
				ClientTask clientTask=new ClientTask();
				clientTask.setOrderNo(new OrderNoGenerator().getString());
				clientTask.setTaskId(task.getId());
				clientTask.setClientId(clientId);
				clientTask.setDepositAmount(task.getDepositAmount());
				clientTask.setPayStatus(1);
				clientTask.setEndTime(OffsetDateTime.now().plusDays(task.getMaxDay()));
				if(clientTaskMapper.insert(clientTask)<=0) {
					Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误,稍后再试");
				}
				ClientTaskCount clientTaskCount=new ClientTaskCount();
				clientTaskCount.setTaskId(task.getId());
				clientTaskCount.setClientId(clientId);
				if(clientTaskCountMapper.insert(clientTaskCount)<=0) {
					Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误,稍后再试");
				}
			}
		}else {
			Shift.fatal(StatusCode.INSUFFICIENT_STOCK, "该任务已罄售");
		}
  		return true;
	}

  	/**
  	 * 根据包含某种类型取消失败任务
  	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean systemCancelTask(Long clientId,TaskTypeEnum taskTypeEnum) {
		Task findTask=new Task();
		findTask.setClaimType(taskTypeEnum);
		List<Task> taskDatas=getClientAccordTask(clientId, taskTypeEnum);
		
		for (Task taskData : taskDatas) {
			ClientTaskCount clientTaskCountObj=taskData.getClientTaskCount();
			
			String reString="";
			String claimValue="";
			
			String[] claimTypes= taskData.getClaimTypes().split("[,]");
			String[] claimValues= taskData.getClaimValues().split("[,]");
			for (int i = 0; i < claimTypes.length; i++) {
				String claimType=claimTypes[i];
				if(Integer.parseInt(claimType)==taskTypeEnum.getCode()){
					claimValue=claimValues[i];
				}
			}
			if(taskTypeEnum==TaskTypeEnum.ONE){
				reString="已经连续签到"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"天";
			}else if(taskTypeEnum==TaskTypeEnum.TWO){
				reString="已经连续每天查看"+clientTaskCountObj.getVideoCount()+"/"+claimValue+"达人视频";
				}else if(taskTypeEnum==TaskTypeEnum.THREE){
					reString="已经累计邀请"+clientTaskCountObj.getInvitationCount()+"/"+claimValue+"好友";
			}else if(taskTypeEnum==TaskTypeEnum.FOUR ){
				reString="已经累计邀请"+clientTaskCountObj.getConsumerCount()+"/"+claimValue+"消费用户";
			}else if(taskTypeEnum==TaskTypeEnum.FIVE){
				reString="已经累计完成"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"消费返利订单";
			}else if(taskTypeEnum==TaskTypeEnum.SIX){ 
				reString="已经累计完成邀请"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"名技能达人";
			}
			ClientTask clientTask=new ClientTask();
			clientTask.setTaskId(taskData.getId());
			clientTask.setClientId(clientId);
			clientTask.setPayStatus(1);
			clientTask.setReason(reString);
			clientTask.setStatus(4);
			if(clientTaskMapper.cancelTask(clientTask)<=0) {
				Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误,稍后再试");
			}
		}
  		return true;
	}
  	
  	
	/**
  	 * 系统取消失败任务
  	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean systemCancelTask(Long taskId,Long clientId,TaskTypeEnum taskTypeEnum) {
		Task findTask=new Task();
		if(taskId==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"参数错误!");
		}
		findTask.setClientId(clientId);
		Task taskData=getClientTask(findTask);
		ClientTaskCount clientTaskCountObj=taskData.getClientTaskCount();
		
		String reString="";
		String claimValue="";
		
		String[] claimTypes= taskData.getClaimTypes().split("[,]");
		String[] claimValues= taskData.getClaimValues().split("[,]");
		for (int i = 0; i < claimTypes.length; i++) {
			String claimType=claimTypes[i];
			if(Integer.parseInt(claimType)==taskTypeEnum.getCode()){
				claimValue=claimValues[i];
			}
		}
		
		if(taskTypeEnum==TaskTypeEnum.ONE){
			reString="已经连续签到"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"天";
		}else if(taskTypeEnum==TaskTypeEnum.TWO){
			reString="已经连续每天查看"+clientTaskCountObj.getVideoCount()+"/"+claimValue+"达人视频";
			}else if(taskTypeEnum==TaskTypeEnum.THREE){
				reString="已经累计邀请"+clientTaskCountObj.getInvitationCount()+"/"+claimValue+"好友";
		}else if(taskTypeEnum==TaskTypeEnum.FOUR ){
			reString="已经累计邀请"+clientTaskCountObj.getConsumerCount()+"/"+claimValue+"消费用户";
		}else if(taskTypeEnum==TaskTypeEnum.FIVE){
			reString="已经累计完成"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"消费返利订单";
		}else if(taskTypeEnum==TaskTypeEnum.SIX){ 
			reString="已经累计完成邀请"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"名技能达人";
		}
		
		ClientTask clientTask=new ClientTask();
		clientTask.setTaskId(taskId);
		clientTask.setClientId(clientId);
		clientTask.setPayStatus(1);
		clientTask.setReason(reString);
		clientTask.setStatus(4);
		if(clientTaskMapper.cancelTask(clientTask)<=0) {
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误,稍后再试");
		}
  		return true;
	}
  	
  	/**
  	 * 撤销领取任务
  	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean cancelClientTask(Long taskId,Long clientId) {
		Task updateTask=new Task();
		updateTask.setId(taskId);
		updateTask.setSignedNumber(-1);
		updateTask.setActualNumber(-1);
		if(taskMapper.updateCount(updateTask)>0) {//更新已购买量成功
			ClientTask clientTask=new ClientTask();
			clientTask.setTaskId(taskId);
			clientTask.setClientId(clientId);
			clientTask.setPayStatus(1);
			clientTask.setStatus(3);
			if(clientTaskMapper.updateTask(clientTask)<=0) {
				Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误,稍后再试");
			}
			SystemAccount systemAccount=new SystemAccount();
			systemAccount.setProductId(taskId);
			systemAccount.setClientId(clientId);
			systemAccount.setAmount(updateTask.getDepositAmount().multiply(new BigDecimal("0.5")));//50%
			if(!systemAccountService.updateAccount(systemAccount)){
				Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "参数错误,稍后再试");
			}
		}else {
			Shift.fatal(StatusCode.INSUFFICIENT_STOCK, "该任务已罄售");
		}
  		return true;
	}
	
	/**
	 * 查询符合条件的任务
	 * @param user
	 * @return
	 */
	public Page<Task> pageTask(Task task) {
		long count = taskMapper.searchCount(task);
		Page<Task> page = new Page<Task>(task.getPageIndex(), task.getPageSize());
		page.setData(taskMapper.searchList(task));
		page.setTotalCount(count);
		return page;
	}
	
	
	/**
	 * 删除后台任务
	 * @param task
	 * @return
	 */
	public Integer deleteTask(Task task){
		
		ClientTask clientTask = new ClientTask();
		clientTask.setTaskId(task.getId());
		
		List<ClientTask> list = clientTaskMapper.find(clientTask);
		if(list!=null&&list.size()>0){
			return 2;
		}else{
		     task.setStatus(1);
			 return taskMapper.update(task);
		}
		
	}
	
	/**
	 * 根据id 查找任务Map对象
	 * @param task
	 * @return
	 */
	public HashMap<String, Object> findMapById(Task task){
		
		HashMap<String, Object> map = taskMapper.findMapById(task);
		if(map!=null){
			ClientTask clientTask = new ClientTask();
			clientTask.setTaskId(task.getId());
		    clientTask.setStatus(2);//正常结束
		    
		    long endCount =  clientTaskMapper.count(clientTask);
		    map.put("endCount", endCount);
		    
		    clientTask.setStatus(3);//用户取消
		    long  userCancelCount =  clientTaskMapper.count(clientTask);
		    
		    clientTask.setStatus(4);//系统取消
		    long sysCancelCount =  clientTaskMapper.count(clientTask);
		    //总共取消数
		    map.put("cancelCount", userCancelCount+sysCancelCount);
			
		}
		
		return map;
	}

}
